class User:
    def __init__(self,username,passwd,money):
        self.username = username
        self.passwd = passwd
        self.money = money

class Goods:
    def __init__(self, classify, product_name,  price, store):
        self.classify = classify
        self.product_name = product_name
        self.price = price
        self.store = store

class DB:
    """
    单例模式
    """
    instance = None

    def __new__(cls, *args, **kwargs):
        if DB.instance is None:
            DB.instance = super().__new__(cls)
        return DB.instance

    def __init__(self):
        self.user_infor = []
        self.user_car = {}

    def save_user(self,user):
        self.user_infor.append(user)

    def save_car(self,user,good):
        if self.user_car.get(user.username):
            self.user_car[user.username].append(good.product_name)
        else:
            self.user_car[user.username] = []
            self.user_car[user.username].append(good.product_name)


    def find_user_by_username(self,username):
        for user in self.user_infor:
            if username == user.username:
                return user
        return None

DB1 = DB()



class info:
    def register_info(self):
        username = input("请输入您的账号：")
        passwd =  input("请输入您的密码：")
        money = int(input("请输入您的金额："))
        return User(username,passwd,money)
    def goods_info(self,good):
        if good == 'apple':
            return Goods('fruits', 'apple', 8, 30)
        elif good == 'orange':
            return Goods('fruits', 'orange', 4, 100)



class Service:
    def __init__(self):
        self.DB1 = DB()
        self.i = info()

    def register(self,user):
        exist_user = self.DB1.find_user_by_username(user.username)
        if exist_user is not None:
            return print('该账号已经存在！')
        self.DB1.save_user(user)
        print('用户注册成功！')

    def login(self,user):
        exist_user = self.DB1.find_user_by_username(user.username)
        if exist_user is None:
            print("用户账号或密码错误")
            return None
        if exist_user.passwd != user.passwd:
            print('用户账号或密码错误')
            return None
        return True

    def buy_goods(self, user, good, weight):
        good = self.i.goods_info(good)
        if weight > good.store:
            print('库存不足')
        else:
            pay = weight * good.price
            if user.money > pay:
                user.money -= pay
                good.store -= weight
                print(f"购买成功，{user.username}剩余{user.money}元、"
                      f"{good.product_name}剩余库存{good.store}")
                self.DB1.save_car(user,good)
            else:
                print("余额不足")
    def show_user_cars(self,user):
        user_car = self.DB1.user_car[user.username]
        print(f"您的购物车里有：{user_car}")



class main:
    def __init__(self):
        self.i = info()
        self.s = Service()
    def start(self):
        while True:
            result = input("请选择要进行的操作： \n  (1)注册\n  (2)登录\n  (3)退出\n:")
            if result == "1":
                user = self.i.register_info()
                self.s.register(user)
            if result == "2":
                user = self.i.register_info()
                login_user = self.s.login(user)
                if login_user is not None:
                    self.shopping(user)
            if result == "3":
                print("退出登录")
                break

    def shopping(self,user):
        while True:
            result = input("请选择要进行的操作：\n (1)购物\n (2)查看购物车\n (3)退出\n:")
            if result == "1":
                good = input("请输入您要购买的商品名称：")
                weight = int(input("请输入您要购买的商品数量："))
                self.s.buy_goods(user,good,weight)
            if result == "2":
                self.s.show_user_cars(user)
            if result == "3":
                break

main = main()
main.start()
